package com.richard.myapplication.smallViewpager;

import androidx.viewpager.widget.ViewPager;

/**
 * **************************************
 * 项目名称:My Application
 *
 * @Author wuzhiguo
 * 邮箱：wuzhiguo@ksjgs.com
 * 创建时间: 2020/6/18     3:01 PM
 * 用途:
 * **************************************
 */
class CycleScrollOnPageChangeListener implements ViewPager.OnPageChangeListener {
    private ViewPager viewPager;
    private int dataSize;

    public CycleScrollOnPageChangeListener(ViewPager viewPager, int dataSize) {
        this.viewPager = viewPager;
        this.dataSize = dataSize;
    }

    @Override
    public void onPageSelected(int arg0) {
    }

    @Override
    public void onPageScrolled(int position, float offset, int offsetPixels) {
        // 检测是否达到首页或者尾页
        if (offset == 0) {
            if (position == 0) {
                viewPager.setCurrentItem(dataSize - 2, false);
            } else if (position == (dataSize - 1)) {
                viewPager.setCurrentItem(1, false);
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
    }
}
